package com.gzw.dms.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gzw.dms.R;
import com.gzw.dms.bean.ShippingOrderInfo;

import java.util.HashMap;
import java.util.List;


/**
 * Created by Administrator on 2016-5-9.
 */
public class NewDpAdapter extends ArrayAdapter<ShippingOrderInfo> {
    private Context mContext;
    private List<ShippingOrderInfo> lsInfo;
    private static HashMap<Integer, Boolean> isSelected;
    private int resourceId;

    public NewDpAdapter(Context context, int textViewResourceId, List<ShippingOrderInfo> objects) {
        super(context, textViewResourceId, objects);
        this.mContext = context;
        this.lsInfo = objects;
        this.resourceId = textViewResourceId;
        isSelected = new HashMap<Integer, Boolean>();
        // 初始化isSelected的数据
        initDate();
    }
    private void initDate() {
        for (int i = 0; i < 300; i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public View getView(final int arg0, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub

//        if (arg0 < 0 || lsInfo == null || arg0 >= lsInfo.size()) {
//
//        }else{
//        initDate();
//        getIsSelected().put(arg0, false);

        ShippingOrderInfo shippingOrderInfo = getItem(arg0);
        LinearLayout userListItem = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resourceId, userListItem, true);

        TextView tv_hybh = (TextView) userListItem.findViewById(R.id.tv_hybh);
        TextView tv_ztj = (TextView) userListItem.findViewById(R.id.tv_ztj);
        TextView tv_zdwz = (TextView) userListItem.findViewById(R.id.tv_zdwz);
        TextView tv_qysj = (TextView) userListItem.findViewById(R.id.tv_qysj);
        TextView tv_chddh = (TextView) userListItem.findViewById(R.id.tv_chddh);
        TextView tv_fhkh = (TextView) userListItem.findViewById(R.id.tv_fhgh);
        TextView tv_shkh = (TextView) userListItem.findViewById(R.id.tv_shgh);
        TextView tv_sx = (TextView) userListItem.findViewById(R.id.tv_sx);
        CheckBox cb = (CheckBox) userListItem.findViewById(R.id.item_cb);

        if (shippingOrderInfo.getShiping_order_num()!=null) {

            tv_hybh.setText("货运编号:" + "  " + shippingOrderInfo.getShiping_order_num());
            tv_ztj.setText("总件数:" + "  " + shippingOrderInfo.getGoods_num());
            tv_zdwz.setText("配送详细目的地:" + "  " + shippingOrderInfo.getEnd_mechanism());
            tv_qysj.setText("起运时间:" + shippingOrderInfo.getSend_time());
            tv_chddh.setText("出货订单号:" + "  " + shippingOrderInfo.getShiping_order_goid());
            tv_fhkh.setText("受理机构:" + "  " + shippingOrderInfo.getSend_mechanism());
            tv_shkh.setText("收货客户:" + "  " + shippingOrderInfo.getReceipt_name());
            if (shippingOrderInfo.getAging_day() != null) {
                if (shippingOrderInfo.getAging_day().equals("0")) {
                    tv_sx.setText("标准时效:" + shippingOrderInfo.getAging_time() + "(当日)");
                } else if (shippingOrderInfo.getAging_day().equals("1")) {
                    tv_sx.setText("标准时效:" + shippingOrderInfo.getAging_time() + "(次日)");
                } else if (shippingOrderInfo.getAging_day().equals("2")) {
                    tv_sx.setText("标准时效:" + shippingOrderInfo.getAging_time() + "(隔日)");
                }
            } else {
                tv_sx.setText("标准时效:");
            }
        }
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                cb.setChecked(isChecked);
                isSelected.put(arg0, isChecked);
            }
        });
       cb.setChecked(getIsSelected().get(arg0));


        // 加载布局文件
//        final  Holder mHolder;
//
//        if (convertView == null) {
//            convertView = View.inflate(mContext, R.layout.item_order, null);
//            mHolder = new Holder();
//            mHolder.tv_hybh = (TextView) convertView.findViewById(R.id.tv_hybh);
//            mHolder.tv_dhlxr = (TextView) convertView.findViewById(R.id.tv_dhlxr);
//            mHolder.tv_ztj = (TextView) convertView.findViewById(R.id.tv_ztj);
//            mHolder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
//
//            convertView.setTag(mHolder);
//        } else {
//            mHolder = (Holder) convertView.getTag();
//        }
//
//        ShippingOrderInfo shippingOrderInfo = lsInfo.get(arg0);
//        mHolder.tv_hybh.setText(shippingOrderInfo.getShiping_order_num());
//        mHolder.tv_dhlxr.setText(shippingOrderInfo.getReceipt_name());
//        mHolder.tv_ztj.setText(String.valueOf(shippingOrderInfo.getGoods_num()));
//        mHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                mHolder.cb.setChecked(isChecked);
//                isSelected.put(arg0, isChecked);
//            }
//        });
//        mHolder.cb.setChecked(getIsSelected().get(arg0));
        return userListItem;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;

    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        NewDpAdapter.isSelected = isSelected;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public ShippingOrderInfo getItem(int arg0) {
        // TODO Auto-generated method stub
        return lsInfo.get(arg0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lsInfo.size();
    }

public static class Holder {
    public TextView tv_hybh;
    public TextView tv_dhlxr;
    public TextView tv_ztj;
    public CheckBox cb;
}

}
